package com.tensquare.search.controller;

import com.tensquare.search.pojo.Article;
import com.tensquare.search.service.ArticleService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;

@RestController
@CrossOrigin
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    /**
     * 插入
     */
    @RequestMapping(method = RequestMethod.POST,produces ={"application/json;charset=UTF-8"})
    public Result saveArticle(@RequestBody Article article){
        articleService.saveArticle(article);
        return new Result(StatusCode.OK,"插入成功",true);
    }

    /**
     * 文章搜搜
     */
    @RequestMapping(value = "/{key}/{page}/{size}",method = RequestMethod.GET)
    public Result findByKey(@PathVariable String key,@PathVariable Integer page,@PathVariable Integer size) throws UnsupportedEncodingException {
        Page<Article> articles = articleService.searchArticle(key, key, page, size);
        return new Result(StatusCode.OK, "搜索成功", true, new PageResult<>(articles.getTotalElements(), articles.getContent()));
    }

}
